﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;

namespace Common
{ /// <summary>
    /// XML 操作基类
    /// **************************************************   
    /// * 开发者:郑德立
    /// * 功能说明:XML操作
    /// **************************************************
    /// </summary>
    public sealed class XmlHelper : IDisposable
    {
        #region 读取XML到DataSet
        /**************************************************
         * 函数名称:GetXml(string XmlPath)
         * 功能说明:读取XML到DataSet
         * 参    数:XmlPath:xml文档路径
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //获取xml路径
         *          DataSet ds = EC.XmlObject.GetXml(xmlPath); //读取xml到DataSet中
         ************************************************/
        /// <summary>
        /// 功能:读取XML到DataSet中
        /// </summary>
        /// <param name="XmlPath">xml路径</param>
        /// <returns>DataSet</returns>
        public static DataSet GetXml(string XmlPath)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(@XmlPath);
            return ds;
        }
        #endregion

        #region 读取xml文档并返回一个节点
        /**************************************************
         * 函数名称:ReadXmlReturnNode(string XmlPath,string Node)
         * 功能说明:读取xml文档并返回一个节点:适用于一级节点
         * 参    数: XmlPath:xml文档路径;Node 返回的节点名称
         * 适应用Xml:<?xml version="1.0" encoding="utf-8" ?>
         *           <root>
         *               <dns1>ns1.everdns.com</dns1>
         *          </root>
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //获取xml路径
         *          Response.Write(XmlObject.ReadXmlReturnNode(xmlPath, "mailmanager"));
         ************************************************/
        /// <summary>
        /// 读取xml文档并返回一个节点:适用于一级节点
        /// </summary>
        /// <param name="XmlPath">xml路径</param>
        /// <param name="NodeName">节点</param>
        /// <returns></returns>
        public static string ReadXmlReturnNode(string XmlPath, string Node)
        {
            XmlDocument docXml = new XmlDocument();
            docXml.Load(@XmlPath);
            XmlNodeList xn = docXml.GetElementsByTagName(Node);
            return xn.Item(0).InnerText.ToString();
        }
        #endregion

        #region 查找数据,返回一个DataSet
        /**************************************************
         * 函数名称:GetXmlData(string xmlPath, string XmlPathNode)
         * 功能说明:查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
         * 参    数:xmlPath:xml文档路径;XmlPathNode:当前节点的路径
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          DataSet ds = new DataSet();
         *          ds = XmlObject.GetXmlData(xmlPath, "root/items");//读取当前路径
         *          this.GridView1.DataSource = ds;
         *          this.GridView1.DataBind();
         *          ds.Clear();
         *          ds.Dispose();
         * Xml示例:
         *         <?xml version="1.0" encoding="utf-8" ?>
         *            <root>
         *              <items name="xinnet">
         *                <url>http://www.paycenter.com.cn/cgi-bin/</url>
         *                <port>80</port>
         *              </items>
         *            </root>
         ************************************************/
        /// <summary>
        /// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
        /// </summary>
        /// <param name="xmlPath">xml文档路径</param>
        /// <param name="XmlPathNode">节点的路径:根节点/父节点/当前节点</param>
        /// <returns></returns>
        public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            DataSet ds = new DataSet();
            StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
            ds.ReadXml(read);
            return ds;
        }
        /// <summary>
        /// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
        /// </summary>
        /// <param name="Xml">xml格式字符串</param>
        /// <param name="XmlpathNode">节点的路径:根节点/父节点/当前节点</param>
        /// <returns></returns>
        public static DataSet GetXmlDataT(string Xml, string XmlpathNode)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.LoadXml(Xml);
            DataSet ds = new DataSet();
            StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlpathNode).OuterXml);
            ds.ReadXml(read);
            return ds;
        }

        #endregion

        #region 更新Xml节点内容
        /**************************************************
         * 函数名称:XmlNodeReplace(string xmlPath,string Node,string Content)
         * 功能说明:更新Xml节点内容
         * 参    数:xmlPath:xml文档路径;Node:当前节点的路径;Content:内容
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          XmlObject.XmlNodeReplace(xmlPath, "root/test", "56789"); //更新节点内容
         ************************************************/
        /// <summary>
        /// 更新Xml节点内容
        /// </summary>
        /// <param name="xmlPath">xml路径</param>
        /// <param name="Node">要更换内容的节点:节点路径根节点/父节点/当前节点</param>
        /// <param name="Content">新的内容</param>
        public static void XmlNodeReplace(string xmlPath, string Node, string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            objXmlDoc.SelectSingleNode(Node).InnerText = Content;
            objXmlDoc.Save(xmlPath);

        }

        #endregion

        #region 删除XML节点和此节点下的子节点
        /**************************************************
         * 函数名称:XmlNodeDelete(string xmlPath,string Node)
         * 功能说明:删除XML节点和此节点下的子节点
         * 参    数:xmlPath:xml文档路径;Node:当前节点的路径;
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          XmlObject.XmlNodeDelete(xmlPath, "root/test"); //删除当前节点
         ************************************************/
        /// <summary>
        /// 删除XML节点和此节点下的子节点
        /// </summary>
        /// <param name="xmlPath">xml文档路径</param>
        /// <param name="Node">节点路径</param>
        public static void XmlNodeDelete(string xmlPath, string Node)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
            objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
            objXmlDoc.Save(xmlPath);
        }

        #endregion

        #region 插入一个节点和此节点的字节点
        /**************************************************
         * 函数名称:XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content)
         * 功能说明:插入一个节点和此节点的字节点
         * 参    数:xmlPath:xml文档路径;MailNode:当前节点的路径;ChildNode:新插入的节点;Element:插入节点的子节点;Content:子节点的内容
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          XmlObject.XmlInsertNode(xmlPath, "root/test","test1","test2","测试内容"); //插入一个节点和此节点的字节点
         * 生成的XML格式为
         *          <test>
         *               <test1>
         *                    <test2>测试内容</test2>
         *                </test1>
         *            </test>
         ************************************************/
        /// <summary>
        /// 插入一个节点和此节点的字节点
        /// </summary>
        /// <param name="xmlPath">xml路径</param>
        /// <param name="MailNode">当前节点路径</param>
        /// <param name="ChildNode">新插入节点</param>
        /// <param name="Element">插入节点的子节点</param>
        /// <param name="Content">子节点的内容</param>
        public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objChildNode.AppendChild(objElement);
            objXmlDoc.Save(xmlPath);
        }

        #endregion

        #region"加密解密XML"
        /// <summary>
        /// 加密Xml
        /// </summary>
        /// <param name="newXml">加密后的Xml文件路径</param>
        /// <param name="oldXml">加密前的xml文件路径</param>
        public void EncodeXml(string newXml, FileStream oldXml)
        {
            byte[] buffer = new byte[1024]; ;
            int readbyte = 0;
            XmlTextWriter xw = new XmlTextWriter(newXml, Encoding.UTF8);
            xw.WriteStartDocument();
            xw.WriteStartElement("root");
            BinaryReader br = new BinaryReader(oldXml);
            do
            {
                readbyte = br.Read(buffer, 0, 1024);
                xw.WriteBase64(buffer, 0, readbyte);
            }
            while (1024 <= readbyte);
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();
        }
        /// <summary>
        /// 解密XMl
        /// </summary>
        /// <param name="oldXml">原xml文件路径</param>
        /// <param name="newXml">解密后的Xml文件路径</param>
        public void decodeXml(string oldXml, FileStream newXml)
        {
            byte[] buffer = new byte[1024]; ;
            int readbyte = 0;
            BinaryWriter bw = new BinaryWriter(newXml);
            XmlTextReader tr = new XmlTextReader(oldXml);
            tr.MoveToContent();
            do
            {
                readbyte = tr.ReadBase64(buffer, 0, 1024);
                bw.Write(buffer, 0, readbyte);
            }
            while (1024 <= readbyte);
            bw.Flush();
            bw.Close();
        }
        #endregion

        #region IDisposable 成员
        public void Dispose()
        {

        }
        #endregion
    }
}
